package au.com.bus4u.controller.admin;


import au.com.bus4u.controller.BaseController;
import au.com.bus4u.service.AuctionService;
import au.com.bus4u.service.EmailService;
import au.com.bus4u.service.OrderService;
import au.com.bus4u.service.UserService;
import au.com.bus4u.utils.JsonResult;
import au.com.bus4u.utils.PageData;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.ws.api.policy.SourceModel;
import lombok.extern.java.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/order")
@Log
public class GuestOrderController extends BaseController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private EmailService emailService;
    @Resource
    private OrderService orderService;
    @Resource
    private AuctionService auctionService;
    @Resource
    private UserService userService;


    /**
     * /admin/menu/add
     */
    @RequiresPermissions("order:add")
    @ResponseBody
    @RequestMapping("/add")
    public JsonResult add() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        orderService.save(getPageData());
        result.setCode(200);
        result.setMsg("添加成功");
        return result;
    }

    @RequiresPermissions("order:delete")
    @ResponseBody
    @RequestMapping("/del")
    public JsonResult deleteMenu(Integer menu_id) throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        orderService.delete(menu_id);
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }

    @RequiresPermissions("order:batchdelete")
    @ResponseBody
    @RequestMapping("/batchDeleteGuestOrder")
    public JsonResult batchDeleteGuestOrder(String[] ids) throws Exception {
        log.info("=========================================");
        for (String id : ids){
            System.err.println("id = [" + id + "]");
        }
        orderService.deleteAll(ids);
        JsonResult result = new JsonResult();
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }

    @RequiresPermissions("order:delete")
    @ResponseBody
    @RequestMapping("/deleteGuestOrder")
    public JsonResult deleteGuestOrder(Integer order_id) throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        result.setCode(200);
        result.setMsg("删除成功");
        orderService.delete(order_id);
        return result;
    }

    @RequiresPermissions("order:update")
    @ResponseBody
    @RequestMapping({"/update","/updateOrder"})
    public JsonResult updateMenu() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        PageData pageData = getPageData();
        orderService.edit(pageData);

        String order_id = pageData.get("order_id").toString();
        Integer order_status = pageData.getInt("order_status");
        String to = to = orderService.getEmailByOrderID(order_id);
        String subjecr = "邮件标题";
        String content = "邮件内容";
        boolean isSend = false;
        switch(order_status){
            case 1://已提交-待审核

                break;
            case 2://待补充
                subjecr = "订单信息待补充";
                content = pageData.getString("email_msg");
                isSend = true;
                break;
            case 3://被驳回
                subjecr = "订单被驳回";
                content = pageData.getString("email_msg");
                isSend = true;
                break;
            case 4://竞拍中

                break;
            case 5://流拍
                subjecr = "订单竞拍失败，无人服务人员参与竞拍";
                content = "订单竞拍失败，无人服务人员参与竞拍";
                isSend = true;
                break;
            case 6://待支付
                subjecr = "订单已经受理，请支付";
                content = "订单已经受理，请支付";
                isSend = true;
                break;
            case 7://支付成功
                subjecr = "订单支付成功";
                content = "订单支付成功";
                isSend = true;
                break;
            case 8://服务中

                break;
            case 9://服务完成

                break;
            case 10://订单取消

                break;
            case 11://客户已修改

                break;
            case 12://司机待确认

                break;
            case 13://司机已确认

                break;
            default:
                break;
        }
        if(isSend){
            emailService.sendMail(to,subjecr,content);
        }


        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }


    /**
     * 查询
     */
    @RequiresPermissions("order:view")
    @ResponseBody
    @RequestMapping("loadAllGuestOrder")
    public PageData loadAllGuestOrder() throws Exception {
        log.info("=========================================");
//        System.err.println("GuestOrderController.loadAllGuestOrder");
        setPageHelperStartPage();//从request中获取pageNum和pageSize并设置到PageHelper.startPage

        PageData pageData = getPageData();
        pageData.put("menu_type",1);
        List<PageData> list = orderService.list(pageData); // 列出Category列表
        PageInfo<PageData> pageInfo = new PageInfo<>(list);//得到分页的结果对象
        List<PageData> pageList = pageInfo.getList();//获取返回页面的分页list数据
        PageData page = getPage(pageInfo);//获取分页数据page

        PageData result = new PageData();
        result.put("code",0);
        result.put("data",pageList);
        result.put("page",page);
        result.put("count",page.get("total"));//总条数 layui数据表分页组件需要

        return result;
    }

    /**
     * 查询   显示该订单的竞拍信息
     */
    @RequiresPermissions("order:showAuctionInfo")
    @ResponseBody
    @RequestMapping("showAuctionInfo")
    public PageData showAuctionInfo(Integer order_id) throws Exception {
        log.info("=========================================");
        PageData result = new PageData();
        List<PageData> auctionList = auctionService.findByOrderId(order_id);
        result.put("auctionList",auctionList);
        result.put("code",200);
        return result;
    }



    /**
     * 确定订单竞拍成功的竞拍人
     * 参数PageData中获取
     *      auction_id      竞拍ID
     *      order_id        订单ID
     *      custo_costs     游客需支付价格
     *
     *      将订单由【竞拍中】改为【待支付】
     *      游客待支付费用保存到订单中
     *      修改竞拍ID 竞拍成功
     */
    @RequiresPermissions("order:confirmAuction")
    @ResponseBody
    @RequestMapping("confirmAuction")
    public PageData confirmAuction() throws Exception {
        log.info("=========================================");
        PageData pd = getPageData();
        orderService.confirmAuction(pd);
        pd.put("code",200);
        return pd;
    }


    /**
     * 查询   显示该订单的服务人员信息(司机、承运商信息)
     * 通过订单ID查询该订单的服务人员信息
     * 如果订单为
     *      内部消化订单 管理员还未指定司机 则无服务人员信息
     *      竞拍订单 竞拍成功情况下 返回服务人员信息为 竞拍成功人的信息
     *          服务人员角色为 司机
     *          服务人员角色为 承运商
     *
     */
    @RequiresPermissions("order:showDiverInfo")
    @ResponseBody
    @RequestMapping("showDiverInfo")
    public PageData showDiverInfo(Integer order_id) throws Exception {
        log.info("=========================================");
        PageData result = new PageData();
        PageData servicePersonal = userService.getServicePersonal(order_id);
        result.put("servicePersonal",servicePersonal);
        result.put("code",200);
        return result;
    }
}
